package com.liaoze.stockmgt.controller;

import java.util.Map;

import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.liaoze.stockmgt.common.controller.BaseController;
import com.liaoze.stockmgt.common.utils.LoggerUtils;
import com.liaoze.stockmgt.common.utils.VerifyCodeUtils;
import com.liaoze.stockmgt.model.UUser;
import com.liaoze.stockmgt.service.UserService;

import net.sf.json.JSONObject;

@Controller
public class UserController extends BaseController{  

    @Resource  
    private UserService userService; 

    /**
     * 首页加载
     * @return user信息
     */
    @RequestMapping("/")    
    public ModelAndView getIndex(){      
        ModelAndView mav = new ModelAndView("index"); 
        UUser user = userService.selectUserById(1); 
        mav.addObject("user", user); 
        return mav;
    }
    
    /**
     * 用户登陆跳转
     * @return
     */
    @RequestMapping(value="login", method=RequestMethod.GET)    
    public ModelAndView login(){      
        return new ModelAndView("sign-in"); 
    }
    
    /**
     * 用户注册跳转
     * @return
     */
    @RequestMapping(value="register", method=RequestMethod.GET)    
    public ModelAndView register(){      
        return new ModelAndView("sign-up");
    }
    
    /**
     * 用户注册 
     * @return
     */
    @ResponseBody
    @RequestMapping(value="subRegister", method=RequestMethod.POST)    
    public  Map<String,Object> subRegister(String vcode, UUser entity){
    	resultMap.put("status", 400);
    	if(!VerifyCodeUtils.verifyCode(vcode)){
			resultMap.put("message", "验证码不正确！");
			return resultMap;
		}
    	
    	
    	
    	LoggerUtils.fmtDebug(getClass(), "注册插入完毕！", JSONObject.fromObject(entity).toString());
    	resultMap.put("status", 200);
        return resultMap;
    }
    
   /**
    * 安全退出
    * @return
    */
    @RequestMapping("logout")    
    public ModelAndView logout(){      
        ModelAndView mav = new ModelAndView("sign-up"); 
        UUser user = userService.selectUserById(1); 
        mav.addObject("user", user); 
        return mav;
    }
}  